﻿using ExcelDna.Integration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ExcelFunc
{
    class RegexUtility
    {
        [ExcelFunction(Description = "字符串拼接函数，dataRange：传入的需要写入目标单元格的数据区域，strSplit：用来分割的字符")]
        public static string RegexMatchGroup(string input, string pattern, int matchNum, int groupNum, bool isCompiled = false, bool isECMAScript = false, bool isRightToLeft = false)
        {
            try
            {
                RegexOptions options = GetRegexOptions(isCompiled, isECMAScript, isRightToLeft);
                MatchCollection matches = Regex.Matches(input, pattern, options);

                if (matchNum <= matches.Count - 1)
                {
                    Match match = matches[matchNum];
                    if (groupNum == 0)
                    {
                        return match.Value;
                    }
                    else
                    {
                        if (groupNum < match.Groups.Count)
                        {
                            return match.Groups[groupNum].Value;
                        }
                        else
                        {
                            return "";
                        }
                    }
                }
                else
                {
                    return "";
                }

            }
            catch (ArgumentOutOfRangeException)
            {
                return "Options错误";
            }
            catch (ArgumentNullException)
            {
                return "";
            }

            catch (ArgumentException)
            {
                return "Pattern错误";
            }
            catch (Exception)
            {
                return "";
            }
        }

        [ExcelFunction(Description = "字符串拼接函数，dataRange：传入的需要写入目标单元格的数据区域，strSplit：用来分割的字符")]
        public static string RegexMatch(string input, string pattern, int matchNum, bool isCompiled, bool isECMAScript, bool isRightToLeft)
        {
            int groupNum = 0;
            return RegexMatchGroup(input, pattern, matchNum, groupNum, isCompiled, isECMAScript, isRightToLeft);
        }

        [ExcelFunction(Description = "字符串拼接函数，dataRange：传入的需要写入目标单元格的数据区域，strSplit：用来分割的字符")]
        public static string RegexReplace(string input, string pattern, string replacement, bool isCompiled = false, bool isECMAScript = false, bool isRightToLeft = false)
        {
            try
            {
                RegexOptions options = GetRegexOptions(isCompiled, isECMAScript, isRightToLeft);
                return Regex.Replace(input, pattern, replacement, options);
            }
            catch (ArgumentOutOfRangeException)
            {
                return "Options错误";
            }
            catch (ArgumentNullException)
            {
                return "";
            }

            catch (ArgumentException)
            {
                return "Pattern错误";
            }
            catch (Exception)
            {
                return input;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="pattern"></param>
        /// <param name="returnNum">索引从0开始</param>
        /// <param name="isCompiled"></param>
        /// <param name="isECMAScript"></param>
        /// <param name="isRightToLeft"></param>
        /// <returns></returns>
        [ExcelFunction(Description = "字符串拼接函数，dataRange：传入的需要写入目标单元格的数据区域，strSplit：用来分割的字符")]
        public static string RegexSplit(string input, string pattern, int returnNum, bool isCompiled = false, bool isECMAScript = false, bool isRightToLeft = false)
        {
            try
            {
                RegexOptions options = GetRegexOptions(isCompiled, isECMAScript, isRightToLeft);
                string[] splitResult = Regex.Split(input, pattern, options);
                if (returnNum <= splitResult.Length - 1)
                {
                    return splitResult[returnNum];
                }
                else
                {
                    return "";
                }

            }
            catch (ArgumentOutOfRangeException)
            {
                return "Options错误";
            }
            catch (ArgumentNullException)
            {
                return "";
            }

            catch (ArgumentException)
            {
                return "Pattern错误";
            }
            catch (Exception)
            {
                return input;
            }
        }

        [ExcelFunction(Description = "字符串拼接函数，dataRange：传入的需要写入目标单元格的数据区域，strSplit：用来分割的字符")]
        public static bool RegexIsMatch(string input, string pattern, bool isCompiled = false, bool isECMAScript = false, bool isRightToLeft = false)
        {
            try
            {
                RegexOptions options = GetRegexOptions(isCompiled, isECMAScript, isRightToLeft);
                return Regex.IsMatch(input, pattern, options);
            }

            catch (Exception)
            {
                return false;
            }
        }

        private static RegexOptions GetRegexOptions(bool isCompiled, bool isECMAScript, bool isRightToLeft)
        {
            List<RegexOptions> listOptions = new List<RegexOptions>();
            if (isCompiled == true)
            {
                listOptions.Add(RegexOptions.Compiled);
            }
            if (isRightToLeft == true)
            {
                listOptions.Add(RegexOptions.RightToLeft);
            }
            if (isECMAScript == true)
            {
                listOptions.Add(RegexOptions.ECMAScript);
            }

            RegexOptions options = new RegexOptions();
            foreach (var item in listOptions)
            {
                if (options == 0)
                {
                    options = item;
                }
                else
                {
                    options = options | item;
                }
            }

            return options;
        }







    }
}
